package com.example.zg.androidnewview.behaviors;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 创建日期:2019/1/10 on 15:42
 * 描述：
 * 作者: zhougan
 */
public class Behavior2 extends CoordinatorLayout.Behavior<View> {
    private static final String TAG = "Behavior2";
    public Behavior2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        //默认隐藏
        if(!isHide){
            hide(child,0);
        }
        return super.layoutDependsOn(parent,child,dependency);
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        //只监听竖直滚动
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
//        Log.i(TAG, "onNestedScroll: dxConsumed="+dxConsumed+",dyConsumed="+dyConsumed+",dxUnconsumed="+dxUnconsumed+",dyUnconsumed="+dyUnconsumed+",type="+type+",target.getScrollY()="+target.getScrollY());

        if(dyConsumed>0 && !isHide){
            hide(child);
        }else if(dyConsumed<0 && isHide){
            show(child);
        }
        //滑动到顶部了
        if(target.getScrollY()==0 && !isHide){
            hide(child);
        }
    }
    private boolean isHide=false;


    private void hide(View view,long duration){
        //动画缩小到0
        view.animate().scaleX(0).scaleY(0).setDuration(duration);
        isHide=true;

    }
    private void hide(View view){
        //动画缩小到0
        view.animate().scaleX(0).scaleY(0).setDuration(200);
        isHide=true;

    }

    private void show(View view){
        //动画还原
        view.animate().scaleX(1).scaleY(1).setDuration(200);
        isHide=false;
    }
}
